﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

public partial class area_admin_equipamentos : System.Web.UI.Page
{
    private FUNCIONARIO funcionarios = new FUNCIONARIO();
    private dbSisInfoDataContext Banco = new dbSisInfoDataContext();

    protected void Page_Load(object sender, EventArgs e)
    {
        Session["Titulo"] = "Equipamentos";

    }

    protected void btnIncluir_Click(object sender, EventArgs e)
    {
      
    }
    protected void grdSituacoes_SelectedIndexChanged(object sender, EventArgs e)
    {

        Button1_ModalPopupExtender.Show();

    }
    protected void btnPesquisar_Click(object sender, EventArgs e)
    {
        linqFuncionarios.Where = "Nome.Contains(\"" + txtDescricao.Text + "\")";
        linqFuncionarios.DataBind();
        grdFuncionarios.DataBind();

    }

    protected void grdEquipamentos_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

    }
    protected void btnSim0_Click(object sender, EventArgs e)
    {

        grdFuncionarios.DeleteRow(grdFuncionarios.SelectedIndex);
        grdFuncionarios.DataBind();
    }
    protected void grdEquipamentos_DataBound(object sender, EventArgs e)
    {
        if (grdFuncionarios.Rows.Count > 0)
        {
            grdFuncionarios.HeaderRow.Cells[0].Text += " - Quantidade de items: " + grdFuncionarios.Rows.Count.ToString();
        }
    }
    protected void grdEquipamentos_RowEditing(object sender, GridViewEditEventArgs e)
    {

    }
    protected void btnNovo_Click(object sender, EventArgs e)
    {
         CreateUserWizard1.Visible = true;
        txtNome.Text = txtDescricao.Text;
        txtNome.Focus();
    }
    protected void btnCancela_Click(object sender, EventArgs e)
    {
        pnlInserir.Visible = false;

        //Limpar os campos
        txtNome.Text = string.Empty;
        txtRG.Text = string.Empty;
        txtCPF.Text = string.Empty;
        txtDataNascimento.Text = string.Empty;
    }

    protected void CreateUserWizard1_CreatedUser(object sender, EventArgs e)
    {
        string[] strUsuario = new string[1];
        strUsuario[0] = CreateUserWizard1.UserName.ToString();
        Roles.AddUsersToRole(strUsuario, "consulta");
        Roles.AddUsersToRole(strUsuario, "tecnico");


    }
    protected void CreateUserWizard1_FinishButtonClick(object sender, WizardNavigationEventArgs e)
    {
        try
        {
            // Preenche os campos
            funcionarios.Nome = txtNome.Text;
            funcionarios.CPF = txtCPF.Text;
            funcionarios.RG = txtRG.Text;
            funcionarios.DataNascimento = Convert.ToDateTime(txtDataNascimento.Text);
            funcionarios.Foto = "fota.jpg";


            var dadosuser = from U in Banco.aspnet_Users
                            where U.UserName == (CreateUserWizard1.UserName.ToString().Trim())
                            select U;      

            funcionarios.UserId = (Guid)dadosuser.ToList<aspnet_User>()[0].UserId;

            Banco.FUNCIONARIOs.InsertOnSubmit(funcionarios);
            Banco.SubmitChanges();
            grdFuncionarios.DataBind();

            txtDescricao.Text = string.Empty;

            pnlInserir.Visible = false;
        }
        catch
        {
            if (!string.IsNullOrEmpty(CreateUserWizard1.UserName))
            {
                Membership.DeleteUser(CreateUserWizard1.UserName);
            }
            else
            {
                CreateUserWizard1.ActiveStepIndex = 2;
                ScriptManager.RegisterClientScriptBlock(this, GetType(), "script", "alert('Ocorreu um erro na criação do usuário! Tente novamente.'); window.location=\"CadParticipante.aspx\"", true);
            }
        }
    }
}
